package com.fqcar.fqdf.core;

import android.content.Intent;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.entity.ResponseEntity;
import com.fqcar.fqdf.ui.NormalHomeActivity;
import com.fqcar.fqdf.util.SharedPreferenceUtil;
import com.fqdf.framework.android.net.response.NetBaseWrapper;
import com.fqdf.framework.android.net.response.NetBeanWrapper;
import com.fqdf.framework.android.util.DialogUtil;

/**
 * Created by 张枭 on 2017/7/31.
 * 需要使用到ButterKnife的Fragment继承这个类
 */
public abstract class BaseRequestFragment extends BaseButterKnifeFragment {

    private Response.ErrorListener defaultErrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //提示网络连接异常
            DialogUtil.ToastMessage(getContext(), getResources().getString(R.string.network_error));
        }
    };

    private Response.ErrorListener closeSwipRefreshErrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (getContext() instanceof SwipLoadMoreActivity) {
                ((SwipLoadMoreActivity) getContext()).closeSwipRefreshAnim();
            }
            //提示网络连接异常
            DialogUtil.ToastMessage(getContext(), getResources().getString(R.string.network_error));
        }
    };

    private Response.ErrorListener hidenProgressErrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            hidenMaterialDialog(getProgressDialog());
            hidenMaterialDialog(getNoCancelProgressDialog());
            hidenMaterialDialog(getNoCancelSubmitDialog());
            hidenMaterialDialog(getUploadImgProgressDialog());

            //提示网络连接异常
            DialogUtil.ToastMessage(getContext(), getResources().getString(R.string.network_error));
        }
    };

    /**
     * 处理登录失效
     */
    protected void executeTokenFailue() {
        SharedPreferenceUtil.clearSP(getContext(), SharedPreferenceUtil.SP_NAME_USER);
        Intent intent = new Intent(getContext(), NormalHomeActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("content", "您的登陆信息已经失效，请重新登陆");
        DialogUtil.ToastMessage(getContext(), "您的登陆信息已经失效，请重新登陆");
        startActivity(intent);
    }

    /**
     * 获取隐藏加载更多的监听
     *
     * @return 关闭刷新的监听
     */
    protected Response.ErrorListener getCloseSwipRefreshErrorListener() {
        return closeSwipRefreshErrorListener;
    }

    /**
     * 获取隐藏progress的默认错误监听
     *
     * @return 隐藏正在处理的监听
     */
    protected Response.ErrorListener getHidenProgressErrorListener() {
        return hidenProgressErrorListener;
    }


    /**
     * 获取默认错误监听
     *
     * @return 错误的监听
     */
    protected Response.ErrorListener getDefaultErrorListener() {
        return defaultErrorListener;
    }

    /**
     * 处理服务器返回结果
     *
     * @param response 服务器响应
     * @return true 返回结果有问题，父类进行接管。 false返回结果正确交给子类处理
     */
    protected boolean handleResponse(ResponseEntity response) {

        if (response == null) {
            //提示服务器给出的错误
            DialogUtil.ToastMessage(getContext(), R.string.server_error);

        } else if (response.code == ResponseEntity.SERVER_NO_EXCEPTION_CODE) {
            return false;
        } else if (response.code == ResponseEntity.TOKEN_FAIL) {
            executeTokenFailue();
        } else {
            //提示服务器给出的错误
            DialogUtil.ToastMessage(getContext(), response.desc);
        }
        return true;
    }

    /**
     * 判断请求响应是否成功
     *
     * @param response 请求返回的数据结构
     * @return true 代表请求成功
     */
    protected boolean isResponseSuccess(ResponseEntity response) {
        return response != null && response.code == ResponseEntity.SERVER_NO_EXCEPTION_CODE;
    }

    protected boolean handleResponse(NetBaseWrapper response) {

        if (response == null) {
            //提示服务器给出的错误
            DialogUtil.ToastMessage(getContext(), R.string.server_error);
        } else if (response.isAllSuccess()) {
            return false;
        } else if (response.isAuthcodeError()) {
            executeTokenFailue();
        } else {
            //提示服务器给出的错误
            DialogUtil.ToastMessage(getContext(), response.getDesc());
        }
        return true;
    }

    /**
     * 判断请求响应是否成功
     * @param response 请求返回的数据结构
     * @return true 代表请求成功
     */
    protected boolean handleBeanResponse(NetBeanWrapper response) {

        if (!handleResponse(response)) {
            if(response.getModelData()==null){
                DialogUtil.ToastMessage(getContext(), R.string.server_bean_error);
            }else{
                return false;
            }
        }

        return true;
    }
}
